var KOC = require("koc.common.utils");

var Config = require("../../config");

var DALFactory = require("../../lib/dal/dal-factory");

module.exports = {
    //region Live:直播相关
    Live: {
        //region Callback:直播通知回调
        Callback: function (req, res) {
            // {
            //     app: '4269.livepush.myqcloud.com',
            //     appname: 'live',
            //     channel_id: 'ROOM_6273_444207e0f6483c31bb294df1177b582c',
            //     event_type: 1,
            //     sign: 'c550c041abfcffbca318ff126e2056a8',
            //     stream_id: 'ROOM_6273_444207e0f6483c31bb294df1177b582c',
            //     t: 1476429797
            // }
            var _parm = {
                EventType: req.params.event_type,
                Timestamp: KOC.StringUtils.ToIntPositive(req.params.t),
                Sign: req.params.sign
            };
            _parm.User_ID = KOC.StringUtils.ToIntPositive(KOC.StringUtils.ToString(req.params.stream_id).replace("ROOM_", "").split("_")[0]);
            _parm.TimeDiff = KOC.Moment().diff(KOC.Moment(_parm.Timestamp * 1000), "minutes");
            //记录LOG
            DALFactory.Log.Qcloud_Log.Insert({
                User_ID: _parm.User_ID,
                Stream_ID: req.params.stream_id,
                EventType: _parm.EventType,
                Time: _parm.Timestamp,
                Timestamp: _parm.TimeDiff,
                Sign: _parm.Sign,
                Sign_Compare: KOC.StringUtils.MD5(Config.Interface.QCloud.Live.APIKey + _parm.Timestamp)
            }, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp({
                        code: 5,
                        message:retValue.message
                    });
                    return;
                }
            });
            //_parm.TimeDiff = KOC.Moment().diff(KOC.Moment(_parm.Timestamp * 1000), "minutes");
            if (_parm.TimeDiff > 10 || _parm.TimeDiff < -10) {
                res.jsonp({
                    code: 1,
                    t: _parm.Timestamp
                });
                return;
            }
            if (KOC.StringUtils.MD5(Config.Interface.QCloud.Live.APIKey + _parm.Timestamp) != _parm.Sign) {
                res.jsonp({
                    code: 2,
                    sign: _parm.Sign
                });
                return;
            }
            if (!KOC.Lodash.includes([0, 1], _parm.EventType)) {
                res.jsonp({
                    code: 3,
                    event_type: _parm.EventType
                });
                return;
            }
            // _parm.User_ID = KOC.StringUtils.ToIntPositive(KOC.StringUtils.ToString(req.params.stream_id).replace("ROOM_", "").split("_")[0]);
            if (_parm.User_ID <= 0) {
                res.jsonp({
                    code: 4,
                    stream_id:req.params.stream_id
                });
                return;
            }
            //修改艺人流信息
            DALFactory.User.User_Anchor.Update_LiveStream({
                User_ID: _parm.User_ID,
                Live_Stream_Status: _parm.EventType
            }, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp({
                        code: 5,
                        message:retValue.message
                    });
                    return;
                }
                if (retValue.returnObject !== 1) {
                    res.jsonp({
                        code: 6,
                        message:retValue.message
                    });
                    return;
                }
                res.jsonp({
                    code: 0
                });
            });
        }
        //endregion
    }
    //endregion
};